package com.znsb.msfq.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.znsb.msfq.R;
import com.znsb.msfq.bean.ShowBillBean;
import com.znsb.msfq.utils.DisplayUtil;
import com.znsb.msfq.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * @name： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/18
 * @modifyTime： 2017/2/18
 * @explain：
 */
public class BrowerAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater mInflater;
    private int size;
    private String picSize;
    private List<ShowBillBean.DataListBean.ImagesBean> images = new ArrayList<>();

    public BrowerAdapter(Context context, int size) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.size = size;
    }

    public BrowerAdapter(Context context, List<ShowBillBean.DataListBean.ImagesBean> images, String picSize) {
        this.context = context;
        this.picSize = picSize;
        mInflater = LayoutInflater.from(context);
        if (images != null)
            this.images = images;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public Object getItem(int position) {
        return images.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ShowBillBean.DataListBean.ImagesBean item = images.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.plugin_published_grida, null);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.item_grida_image);
            viewHolder.img.setBackgroundColor(Color.rgb(230, 230, 230));
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (getCount() == 1) {// 只有一张图片
            ViewGroup.LayoutParams p = viewHolder.img.getLayoutParams();
            p.width = DisplayUtil.dip2px(100);
            p.height = DisplayUtil.dip2px(100);
//            if (!TextUtils.isEmpty(picSize) && picSize.contains(",")) {
//                String[] size = picSize.split(",");
//                String widthStr = size[0].split("[.]")[0];
//                String heightStr = size[1].split("[.]")[0];
//                int width = 0;
//                int height = 0;
//                try {
//                    width = Integer.parseInt(widthStr);
//                    height = Integer.parseInt(heightStr);
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                }
//                if (width < height) {// 竖屏
//                    p.width = DisplayUtil.dip2px(120);
//                    p.height = DisplayUtil.dip2px(180);
//                } else {// 横屏
//                    p.width = DisplayUtil.dip2px(180);
//                    p.height = DisplayUtil.dip2px(120);
//                }
//            }
            viewHolder.img.setLayoutParams(p);
            viewHolder.img.setScaleType(ImageView.ScaleType.FIT_XY);
        } else {
            ViewGroup.LayoutParams p = viewHolder.img.getLayoutParams();
            p.width = DisplayUtil.dip2px(100);
            p.height = DisplayUtil.dip2px(100);
            viewHolder.img.setLayoutParams(p);
            viewHolder.img.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        ImageLoader.imageLoder(viewHolder.img, item.getUrl());
//        viewHolder.img.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                ArrayList<String> imgs = new ArrayList<String>();
////                for (int i = 0; i < images.size(); i++) {
////                    imgs.add(images.get(i).getUrl());
////                }
////                Intent intent = new Intent(ImgBrowerPagerActivity.class);
////                intent.putStringArrayListExtra(Constant.KEY_VALUE, imgs);
////                intent.putExtra("position", position);
////                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    class ViewHolder {
        private ImageView img;
    }
}

